package org.point.common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {

    private static final Configuration CONFIGURATION = new Configuration();

    public static String execute(Configuration config, String modelName,
            String encoding, Map<?, ?> root,String rootPath) throws IOException,
            TemplateException {
    	CONFIGURATION.setDirectoryForTemplateLoading(new File(rootPath+"WEB-INF/ftl/static"));
        BufferedWriter writer = null;
        try {
            Template tp = config.getTemplate(modelName, encoding);

            StringWriter stringWriter = new StringWriter();
            writer = new BufferedWriter(stringWriter);
            tp.setEncoding(encoding);
            tp.process(root, writer);

            return stringWriter.toString();
        } finally {
            if(null!=writer){
            	 writer.flush();
                 writer.close();
            }
        }
    }

    public static void execute(Configuration config, String modelName,
            String encoding, Map<?, ?> root, FileOutputStream outputStream)throws TemplateException, IOException {
        Writer out = null;
        try {
            out = new OutputStreamWriter(outputStream, encoding);
            config.getTemplate(modelName, encoding).process(root, out);
        } finally {
            try {
                out.flush();
                out.close();
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void execute(Configuration config, String name,
            String encoding, Map<?, ?> root, File file)throws TemplateException, IOException {
        execute(config, name, encoding, root, new FileOutputStream(file));
    }

    public static void execute2(Configuration config, String modelName,
            String encoding, Map<?, ?> root, String filePathName)throws TemplateException, IOException {
        execute(config, modelName, encoding, root, new FileOutputStream(
                filePathName));
    }

    public static void execute2(String modelName, String encoding,
            Map<?, ?> root, String filePathName) throws TemplateException, IOException {
        execute(CONFIGURATION, modelName, encoding, root, new FileOutputStream(
                filePathName));
    }

    public static String execute(String modelName, String encoding,
            Map<?, ?> root,String rootPath) throws IOException, TemplateException {
        return execute(CONFIGURATION, modelName, encoding, root,rootPath);
    }
}
